// ipcHandlers.js
import { ipcMain } from 'electron'
import db, {
  selectBackupFolder,
  backupDatabase,
  selectDatabaseFile,
  importDatabase,
  clearDatabase
} from './db' // 导入数据库操作函数

// 定义所有的 IPC 处理逻辑
function setupIpcHandlers() {
  // 在 IPC 中监听备份请求
  ipcMain.handle('backup-database', backupDatabase)
  ipcMain.handle('selectBackupFolder', selectBackupFolder)
  // 导入数据
  ipcMain.handle('selectDatabaseFile', selectDatabaseFile)
  ipcMain.handle('importDatabase', importDatabase)

  //清除所有数据
  ipcMain.handle('clearDatabase', clearDatabase)

  ipcMain.handle('insert-dealer-level', (event, dealerLevelName, isDividend) =>
    db.insertDealerLevel(dealerLevelName, isDividend)
  )
  ipcMain.handle('get-all-dealer-level', () => db.getAllDealerLevel())
  ipcMain.handle('deleteDealerLevel', (event, dealerLevelId) => db.deleteDealerLevel(dealerLevelId))
  ipcMain.handle('editDealerLevel', (event, dealerLevelId, dealerLevelName, isDividend) =>
    db.editDealerLevel(dealerLevelId, dealerLevelName, isDividend)
  )

  ipcMain.handle(
    'insertDealer',
    (
      event,
      parentDealerId,
      parentDealerName,
      dealerLevelId,
      dealerLevelName,
      dealerName,
      dealerAddress,
      dealerMobile,
      dealerRemark
    ) =>
      db.insertDealer(
        parentDealerId,
        parentDealerName,
        dealerLevelId,
        dealerLevelName,
        dealerName,
        dealerAddress,
        dealerMobile,
        dealerRemark
      )
  )
  ipcMain.handle('getDealerList', (event, pageSize, offset, dealerLevelId, dealerName) =>
    db.getDealerList(pageSize, offset, dealerLevelId, dealerName)
  )
  ipcMain.handle('getDealerListCount', (event, dealerId) => db.getDealerListCount(dealerId))
  ipcMain.handle('updateDealerLevel', (event, dealerLevelId, dealerLevelName, dealerId) =>
    db.updateDealerLevel(dealerLevelId, dealerLevelName, dealerId)
  )
  ipcMain.handle('updateDealerRemark', (event, dealerRemark, dealerId) =>
    db.updateDealerRemark(dealerRemark, dealerId)
  )
  ipcMain.handle('deleteDealer', (event, dealerId, parentId, parentName) =>
    db.deleteDealer(dealerId, parentId, parentName)
  )
  ipcMain.handle('getDealerAll', (event, dealerLevelId, dealerName) =>
    db.getDealerAll(dealerLevelId, dealerName)
  )
  ipcMain.handle(
    'updateDealer',
    (
      event,
      dealerId,
      parentDealerId,
      parentDealerName,
      dealerLevelId,
      dealerLevelName,
      dealerName,
      dealerAddress,
      dealerMobile,
      dealerRemark
    ) =>
      db.updateDealer(
        dealerId,
        parentDealerId,
        parentDealerName,
        dealerLevelId,
        dealerLevelName,
        dealerName,
        dealerAddress,
        dealerMobile,
        dealerRemark
      )
  )

  //分红规则
  ipcMain.handle('insertDividend', (event, dividendName, isEnable, ratioList) =>
    db.insertDividend(dividendName, isEnable, ratioList)
  )
  ipcMain.handle('getDividendAll', () => db.getDividendAll())
  ipcMain.handle('getDividendByName', (event, dividendId, dividendName) =>
    db.getDividendByName(dividendId, dividendName)
  )
  ipcMain.handle('getDividendDetail', (event, dividendId) => db.getDividendDetail(dividendId))
  ipcMain.handle(
    'updateDividend',
    (event, dividendId, dividendName, isEnable, addRatioList, updateRatioList, delRatioList) =>
      db.updateDividend(
        dividendId,
        dividendName,
        isEnable,
        addRatioList,
        updateRatioList,
        delRatioList
      )
  )
  ipcMain.handle('getDividendByIsEnable', (event, isEnable) => db.getDividendByIsEnable(isEnable))

  //业绩
  ipcMain.handle(
    'insertPerformance',
    (
      event,
      dealerId,
      dealerName,
      dealerParentId,
      dealerParentName,
      dealerLevelId,
      dealerLevelName,
      performanceDate,
      performanceMoney
    ) =>
      db.insertPerformance(
        dealerId,
        dealerName,
        dealerParentId,
        dealerParentName,
        dealerLevelId,
        dealerLevelName,
        performanceDate,
        performanceMoney
      )
  )
  ipcMain.handle(
    'getPerformanceList',
    (event, pageSize, offset, startDate, endDate, dealerName, dealerLevelId) =>
      db.getPerformanceList(pageSize, offset, startDate, endDate, dealerName, dealerLevelId)
  )
  ipcMain.handle('getPerformanceExport', (event, startDate, endDate, dealerName, dealerLevelId) =>
    db.getPerformanceExport(startDate, endDate, dealerName, dealerLevelId)
  )
  ipcMain.handle(
    'getDividendList',
    (event, pageSize, offset, startDate, endDate, dealerName, dealerLevelId) =>
      db.getDividendList(pageSize, offset, startDate, endDate, dealerName, dealerLevelId)
  )
  ipcMain.handle('getDealerPerformance', (event, dealerId, startDate, endDate) =>
    db.getDealerPerformance(dealerId, startDate, endDate)
  )
  ipcMain.handle('getDealerPersonPerformance', (event, dealerId, startDate, endDate) =>
    db.getDealerPersonPerformance(dealerId, startDate, endDate)
  )
  //日志
  ipcMain.handle('insertLogger', (event, msg) => db.insertLogger(msg))
  ipcMain.handle('getLogger', (event, pageSize, offset) => db.getLogger(pageSize, offset))

  //业绩记录
  ipcMain.handle('getPerformanceRecordByDealerForDate', (event, dealerId, startDate, endDate) =>
    db.getPerformanceRecordByDealerForDate(dealerId, startDate, endDate)
  )

  //等级变更
  ipcMain.handle('getDealerLevelChangeRecord', (event, dealerId) =>
    db.getDealerLevelChangeRecord(dealerId)
  )
}

export { setupIpcHandlers }
